#include "talisman.h"
#include <QtGui>
#include "Map\Space.h"
#include "Map\Map.h"
#include "Map\PositionKeeper.h"
#include "Character\PlayerCharacter.h"
#include "PlayerArea.h"
#include <windows.h>
#include "Instruction.h"

/* Moving the character forward according to the dice roll*/
void Talisman::goForward()
{
	if (mPositionKeeper->getCharacterLocation(mPlayer) == 0) 
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("The player is not on the map. Please perform setup first!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}

	int moves = (mPlayer->isToad() ? 1 : die.read());	
	for (int i = 0; i < moves; ++i)
	{
		Sleep(300);
		mPositionKeeper->advanceCharacter(mPlayer, 1);
		map->updateContents();
		qApp->processEvents();
	}

	Sleep(300);

	map->updateContents();
	showInfo();

	mPlayer->updateToad();
	mPlayer->update();
	map->updateContents();
	map->update();
	mArea->update();
}
/* Moving the character backward according to the dice roll*/
void Talisman::goBack()
{
	if (mPositionKeeper->getCharacterLocation(mPlayer) == 0) 
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("The player is not on the map. Please perform setup first!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}

	int moves = (mPlayer->isToad() ? 1 : die.read());	
	for (int i = 0; i < moves; ++i)
	{
		Sleep(300);
		mPositionKeeper->movebackCharacter(mPlayer, 1);
		map->updateContents();
		qApp->processEvents();
	}

	Sleep(300);
	map->updateContents();
	showInfo();

	mPlayer->updateToad();
	mPlayer->update();
	map->updateContents();
	map->update();
	mArea->update();
}


/*Landing on the bridge, if the character is already on the appropriate space.*/
void Talisman::takeBridge()
{
	if (mPositionKeeper->getCharacterLocation(mPlayer) == 0) 
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("The player is not on the map. Please perform setup first!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}

	talisman_map::Space * prev = mPositionKeeper->getCharacterLocation(mPlayer);
	mPositionKeeper->takeTheBridge(mPlayer);
	if (mPositionKeeper->getCharacterLocation(mPlayer) == prev)
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("You're not at the bridge yet !"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
	}
	else
	{
		map->updateContents();
		showInfo();
	}
}

/*Landing on the portal, if the character is already on the appropriate space.*/
void Talisman::usePortal()
{
	if (mPositionKeeper->getCharacterLocation(mPlayer) == 0) 
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("The player is not on the map. Please perform setup first!"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
		return;
	}

	talisman_map::Space * prev = mPositionKeeper->getCharacterLocation(mPlayer);
	mPositionKeeper->useThePortal(mPlayer);
	if (mPositionKeeper->getCharacterLocation(mPlayer) == prev)
	{
		QMessageBox msgBox;
		QString msg = "";
		msg.append(QString("You're not at the portal yet !"));
		msgBox.setText(msg);
		msgBox.setIcon(QMessageBox::Information);
		msgBox.exec();
	}
	else
	{
		map->updateContents();
		showInfo();
	}
}